package org.example.ttms.controller;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.RowConstraints;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import org.example.ttms.Main;
import org.example.ttms.customize.Pop_ups;
import org.example.ttms.entityClass.Account;
import org.example.ttms.entityClass.Manager;
import org.example.ttms.entityClass.User;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class EnrollController {

    @FXML
    private Button enroll;

    @FXML
    private PasswordField password;

    @FXML
    private TextField account;

    @FXML
    private PasswordField password2;

    @FXML
    private ChoiceBox<String> choiceBox;

    @FXML
    private Button returnButton;

    private String[] typeAll = {"管理员", "用户"};

    public Button getEnroll() {
        return enroll;
    }

    public void setEnroll(Button enroll) {
        this.enroll = enroll;
    }

    public PasswordField getPassword() {
        return password;
    }

    public void setPassword(PasswordField password) {
        this.password = password;
    }

    public TextField getAccount() {
        return account;
    }

    public void setAccount(TextField account) {
        this.account = account;
    }

    public PasswordField getPassword2() {
        return password2;
    }

    public void setPassword2(PasswordField password2) {
        this.password2 = password2;
    }

    public ChoiceBox<String> getChoiceBox() {
        return choiceBox;
    }

    public void setChoiceBox(ChoiceBox<String> choiceBox) {
        this.choiceBox = choiceBox;
    }

    public Button getReturnButton() {
        return returnButton;
    }

    public void setReturnButton(Button returnButton) {
        this.returnButton = returnButton;
    }

    public String[] getTypeAll() {
        return typeAll;
    }

    public void setTypeAll(String[] typeAll) {
        this.typeAll = typeAll;
    }

    public void initialize() {
        choiceBox.getItems().addAll(typeAll);
        choiceBox.getSelectionModel().select(1);
    }
    public String getType(){
        return choiceBox.getValue();
    }

    @FXML
    void back(ActionEvent event) throws IOException {
        Main.jumpPage("login.fxml");
    }

    //判断两次密码是否一致
    public boolean isSame(){
        String p1 = password.getText();
        String p2 = password2.getText();
        return p1.equals(p2);
    }
    //注册评判
    public boolean enrollJudgement(){
        Account account1 = new Account();
        Pop_ups popUps = new Pop_ups();
        //请输入账号
        if(account.getText().isEmpty()){
            popUps.pop_ups1(Main.scene, "请输入账号");
            return false;
        }
        //账号已存在
        if(account1.accountIsExist(account.getText())){
            popUps.pop_ups1(Main.scene, "账号已存在");
            return false;
        }
        //两次密码不一致
        if(!isSame()){
            popUps.pop_ups1(Main.scene, "密码不一致");
            return false;
        }
        //密码没有输入
        if(password.getText().isEmpty() && password2.getText().isEmpty()){
            popUps.pop_ups1(Main.scene, "密码没有输入");
            return false;
        }
        //密码已存在
        if(account1.passwordIsExist(password.getText())){
            popUps.pop_ups1(Main.scene, "密码已存在");
            return false;
        }
        return true;
    }

    @FXML
    void chickEnroll(ActionEvent event) throws IOException {
        if(!enrollJudgement())
            return;
        Pop_ups popUps = new Pop_ups();
        String type = getType();
        if(type.equals("管理员")){
            Stage stage = new Stage();
            stage.getIcons().add(new Image("C:\\project\\trainTicketManagementSystem\\TTMS\\src\\main\\resources\\org\\example\\ttms\\image\\1.png"));
            TextField textField = new TextField();
            textField.setPromptText("请输入激活码");
            textField.setFont(new Font(20));
            textField.setMaxSize(300, 30);
            Text text = new Text("激活码:");
            text.setFont(new Font(20));
            Button button = new Button("确定");
            GridPane gridPane = new GridPane();
            gridPane.getRowConstraints().add(new RowConstraints(50));
            gridPane.add(text, 0, 2);
            gridPane.add(textField, 1, 2);
            gridPane.add(button, 1, 3);
            gridPane.setAlignment(Pos.CENTER);
            GridPane.setMargin(button, new Insets(20, 0, 0, 50));
            Scene scene = new Scene(gridPane, 400,200);
            scene.getStylesheets().add(Objects.requireNonNull(getClass().getResource("/org/example/ttms/css/style.css")).toExternalForm());
            button.getStyleClass().add("button1");
            stage.setScene(scene);
            stage.show();
            button.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent actionEvent) {
                    if(textField.getText().equals("123")){
                        Manager manager = new Manager();
                        String id = manager.obtainId();
                        Account account1 = new Account(account.getText(), password.getText(), "管理员", id);
                        try {
                            account1.enrollAccount(account1);
                            manager = new Manager(id, account.getText(), password.getText());
                            manager.addManager(manager);
                            stage.close();
                            Main.jumpPage("login.fxml");
                            popUps.pop_ups2(Main.scene, "注册成功");
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    else{
                        popUps.pop_ups1(scene, "激活码错误");
                    }
                }
            });
        }
        else{
            User user = new User();
            String id = user.obtainId();
            Account account1 = new Account(account.getText(), password.getText(), "用户", id);
            user = new User(id, account.getText(), password.getText());
            account1.enrollAccount(account1);
            user.addUser(user);
            Main.jumpPage("login.fxml");
            popUps.pop_ups2(Main.scene, "注册成功");
        }
    }
}
